package com.sam.gui;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import com.sam.MainController;
import com.sam.db.DatabaseLog;

public class GUI extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private Button startServer;
	private Button log1;
	private TextField text;
	private Label	label;
	private Label	label2;
	private Button log3;
	DatabaseLog logs;
	int i = 0;

	public GUI()
	{
		setLayout(new FlowLayout());

		setVisible(true);
		label = new Label("Video Directory:");
		add(label);
		text = new TextField(1);
		text.setText("");
		add(text);
		label2 = new Label(":\\xampp\\htdocs\\video");
		add(label2);
		
		startServer = new Button("Start Server");
		add(startServer);
		startServer.addActionListener(this);
		log1 = new Button("Alert Log");
		add(log1);
		log1.addActionListener(this);
		log3 = new Button("Video Log");
		add(log3);
		log3.addActionListener(this);

		setTitle("SaM Server");
		setSize(300,250);
		setVisible(true);
	}

	public static void main(String[] args)
	{
		GUI frame = new GUI();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		File file = new File("sam_launcher.png");
	    Image image=null;
		try
		{
			image = ImageIO.read(file);
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		frame.setIconImage(image);
		frame.setVisible(true);

	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent evt)
	{
		// TODO Auto-generated method stub
		String command = evt.getActionCommand();
		System.out.println("" + command);
		if (command.equals("Start Server"))
		{
		  
		  if(!text.getText().equals("")){	
		  String direct = text.getText();
          MainController obj = MainController.getInstance();
          String vidPath=direct+"://xampp//htdocs//video//";
          
          obj.startFactroy(vidPath);
          startServer.disable();
		  }
		}
		else
		{
			if (i == 0)
			{
				logs = new DatabaseLog();
				
				File file = new File("sam_launcher.png");
			    Image image=null;
				try
				{
					image = ImageIO.read(file);
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				logs.setIconImage(image);
				i++;
			}

			if (command.equals("Alert Log"))
			{
				logs.showDatabaseLog("sam_alert_log");
				logs.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				logs.setTitle("Alerts Logs");
				logs.pack();
				logs.setVisible(true);
			}
			else if (command.equals("Video Log"))
			{

				logs.showDatabaseLog("sam_video_log");
				logs.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				logs.pack();
				logs.setTitle("Video Logs");
				logs.setVisible(true);

			}
			
		}

	}
}
